#include "mainmenutree.h"

#include <gccore.h>
#include <wiiuse/wpad.h>

//Main Menu options
extern u8 new_game_png[];
extern u8 level_builder_png[];
extern u8 help_png[];
extern u8 exit_png[];

//Game options
extern u8 timed_game_png[];
extern u8 least_portals_game_png[];
extern u8 back_png[];

//Sub Game Options
extern u8 return_png[];
extern u8 restart_png[];
extern u8 quit_png[];

//Builder Options
extern u8 new_png[];
extern u8 load_png[];
//DUPE extern u8 back_png[];

//Sub Builder Options
extern u8 test_png[];
extern u8 save_png[];
//DUPE extern u8 load_png[];
//DUPE extern u8 quit_png[];

//MOUSE OVER IMAGES

extern u8 new_game_over_png[];
extern u8 level_builder_over_png[];
extern u8 help_over_png[];
extern u8 exit_over_png[];

//Game options
extern u8 story_over_png[];
extern u8 story_png[];
extern u8 timed_game_over_png[];
extern u8 least_portals_game_over_png[];
extern u8 back_over_png[];

//Sub Game Options
extern u8 return_over_png[];
extern u8 restart_over_png[];
extern u8 quit_over_png[];

//Builder Options
extern u8 new_over_png[];
extern u8 load_over_png[];
//DUPE extern u8 back_png[];

//Sub Builder Options
extern u8 test_over_png[];
extern u8 save_over_png[];
//DUPE extern u8 quit_png[];

extern u8 next_level_png[];
extern u8 next_level_over_png[];

MainMenuTree::MainMenuTree()
{

	Image* newGameImg      = new Image();
	Image* levelBuilderImg = new Image();
	Image* helpImg         = new Image();
	Image* exitImg         = new Image();
	
	Image* storyImg        = new Image();
	Image* timedImg        = new Image();
	Image* leastPortalsImg = new Image();
	Image* backImg         = new Image();
	
	Image* returnImg       = new Image();
	Image* restartImg      = new Image();
	Image* quitImg         = new Image();
	
	Image* newImg        = new Image();
	Image* loadImg       = new Image();
	
	Image* testImg       = new Image();
	Image* saveImg       = new Image();
	
	Image* nextLevelImg       = new Image();
	
	if(newGameImg->LoadImage(new_game_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(levelBuilderImg->LoadImage(level_builder_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(helpImg->LoadImage(help_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(exitImg->LoadImage(exit_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);

	if(storyImg->LoadImage(story_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(timedImg->LoadImage(timed_game_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(leastPortalsImg->LoadImage(least_portals_game_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(backImg->LoadImage(back_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);

	if(returnImg->LoadImage(return_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(restartImg->LoadImage(restart_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(quitImg->LoadImage(quit_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);

	if(newImg->LoadImage(new_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(loadImg->LoadImage(load_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);

	if(testImg->LoadImage(test_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(saveImg->LoadImage(save_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);

	if(nextLevelImg->LoadImage(next_level_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);

	Image* newGameImg_over      = new Image();
	Image* levelBuilderImg_over = new Image();
	Image* helpImg_over         = new Image();
	Image* exitImg_over         = new Image();
	
	Image* storyImg_over        = new Image();
	Image* timedImg_over        = new Image();
	Image* leastPortalsImg_over = new Image();
	Image* backImg_over         = new Image();
	
	Image* returnImg_over       = new Image();
	Image* restartImg_over      = new Image();
	Image* quitImg_over         = new Image();
	
	Image* newImg_over        = new Image();
	Image* loadImg_over       = new Image();
	
	Image* testImg_over       = new Image();
	Image* saveImg_over       = new Image();
	
	Image* nextLevelImg_over       = new Image();
	
	if(newGameImg_over->LoadImage(new_game_over_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(levelBuilderImg_over->LoadImage(level_builder_over_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(exitImg_over->LoadImage(exit_over_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(helpImg_over->LoadImage(help_over_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);

	if(storyImg_over->LoadImage(story_over_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(timedImg_over->LoadImage(timed_game_over_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(leastPortalsImg_over->LoadImage(least_portals_game_over_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(backImg_over->LoadImage(back_over_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);

	if(returnImg_over->LoadImage(return_over_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(restartImg_over->LoadImage(restart_over_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(quitImg_over->LoadImage(quit_over_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);

	if(newImg_over->LoadImage(new_over_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(loadImg_over->LoadImage(load_over_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);

	if(testImg_over->LoadImage(test_over_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(saveImg_over->LoadImage(save_over_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	
	if(nextLevelImg_over->LoadImage(next_level_over_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);

    for(int i = 0; i < MyMenu::SIZE; i++)
	{
		_menuList.push_back(new Menu() );
	}
	
	_activeMenu = MyMenu::MAIN;
	
	_menuList[MyMenu::MAIN]->AddOption(X_MENU_POS, 220, newGameImg, newGameImg_over);
	_menuList[MyMenu::MAIN]->AddOption(X_MENU_POS, 280, levelBuilderImg, levelBuilderImg_over);
	_menuList[MyMenu::MAIN]->AddOption(X_MENU_POS, 360, helpImg, helpImg_over);
	_menuList[MyMenu::MAIN]->AddOption(X_MENU_POS, 410, exitImg, exitImg_over);

	_menuList[MyMenu::BUILDER]->AddOption(X_MENU_POS, 250, newImg, newImg_over);
	_menuList[MyMenu::BUILDER]->AddOption(X_MENU_POS, 310, loadImg, loadImg_over);
	_menuList[MyMenu::BUILDER]->AddOption(X_MENU_POS, 370, backImg, backImg_over);
	
	_menuList[MyMenu::GAME]->AddOption(X_MENU_POS, 220, storyImg, storyImg_over);
	_menuList[MyMenu::GAME]->AddOption(X_MENU_POS, 280, timedImg, timedImg_over);
	_menuList[MyMenu::GAME]->AddOption(X_MENU_POS, 340, leastPortalsImg, leastPortalsImg_over);
	_menuList[MyMenu::GAME]->AddOption(X_MENU_POS, 400, backImg, backImg_over);
	
	_menuList[MyMenu::SUB_BUILDER]->AddOption(X_MENU_POS, 220, returnImg, returnImg_over);
	_menuList[MyMenu::SUB_BUILDER]->AddOption(X_MENU_POS, 280, testImg, testImg_over);
	_menuList[MyMenu::SUB_BUILDER]->AddOption(X_MENU_POS, 340, saveImg, saveImg_over);
	_menuList[MyMenu::SUB_BUILDER]->AddOption(X_MENU_POS, 400, quitImg, quitImg_over);
	
	_menuList[MyMenu::SUB_GAME]->AddOption(X_MENU_POS, 250, returnImg, returnImg_over);
	_menuList[MyMenu::SUB_GAME]->AddOption(X_MENU_POS, 310, restartImg, restartImg_over);
	_menuList[MyMenu::SUB_GAME]->AddOption(X_MENU_POS, 370, quitImg, quitImg_over);
	
	_menuList[MyMenu::SUB_TEST]->AddOption(X_MENU_POS, 250, returnImg, returnImg_over);
	_menuList[MyMenu::SUB_TEST]->AddOption(X_MENU_POS, 310, restartImg, restartImg_over);
	_menuList[MyMenu::SUB_TEST]->AddOption(X_MENU_POS, 370, quitImg, quitImg_over);

	_menuList[MyMenu::WON_GAME]->AddOption(X_MENU_POS, 300, nextLevelImg, nextLevelImg_over);
	_menuList[MyMenu::WON_GAME]->AddOption(X_MENU_POS, 360, quitImg, quitImg_over);
}

MainMenuTree::~MainMenuTree()
{

}
		
bool MainMenuTree::Update(Pointer* ptr)
{
	
	WPAD_ScanPads();
		
	u16 btnsdown = WPAD_ButtonsDown(0);
	
	_menuList[_activeMenu]->MouseOver(ptr->GetCenter());
	
	if(btnsdown & WPAD_BUTTON_UP)
	{
		_menuList[_activeMenu]->SelectPrevious();
	}

	if(btnsdown & WPAD_BUTTON_DOWN)
	{
		_menuList[_activeMenu]->SelectNext();
	}

	if(btnsdown & WPAD_BUTTON_A)
	{
		return true;
	}
	
	return false;
}
		
void MainMenuTree::ChangeActiveMenu(MyMenu::Type menu, bool transition)
{
	_lastMenu   = _activeMenu;
	_activeMenu = menu;
	
	if(transition)
	{
		_menuList[_lastMenu]->TransitionOut();
	}
	
	_menuList[_activeMenu]->TransitionIn();
}
		
MyMenu::Type MainMenuTree::GetCurrentActiveMenu()
{
	return _activeMenu;
}
		
int MainMenuTree::GetSelectedItem()
{
	return _menuList[_activeMenu]->GetSelected();
}

void MainMenuTree::Render()
{
	/*
	if(_menuList[_lastMenu]->DoTransitionOut())
	{
		_menuList[_lastMenu]->Render();
	}
	else
	*/
	{
		_menuList[_activeMenu]->Render();
	}
}
